package com.itchu.auth.config;


import com.itchu.common.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.security.PrivateKey;
import java.security.PublicKey;

@Data
@Slf4j
@ConfigurationProperties(prefix = "music.jwt")
public class JwtProperties {

    private String pubKeyPath;
    private String priKeyPath;

    private PublicKey publicKey;
    private PrivateKey privateKey;

    // 对象的名字一定要叫user，否则不能注入
    private JwtUser user = new JwtUser();

    @Data
    public class JwtUser {
        private Integer expire;
        private Integer refreshTime;
        private String cookieName;
        private String cookieDomain;
        private String authCookieName;
    }

    /**
     * 因为ioc容器先创建，然后就初始化属性，但有两个属性还没有值，
     * 所有需要在初始化属性之后的生命周期执行此方法
     * @throws Exception
     */
    @PostConstruct
    public void initKey() throws Exception {
        publicKey = RsaUtils.getPublicKey(pubKeyPath);
        privateKey = RsaUtils.getPrivateKey(priKeyPath);
    }
}
